<template>
    <el-dialog :title="!dataForm.id ? '添加日程' : '编辑日程'" :close-on-click-modal="false"
      :visible.sync="visible" class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
      <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="100px">
        <el-form-item label="起始时间" prop="startTime">
          <el-date-picker v-model="dataForm.startTime" type="date" placeholder="选择日期时间"
            :editable="false" :clearable="false" format="yyyy-MM-dd" value-format="timestamp">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker v-model="dataForm.endTime" type="date" placeholder="选择日期时间"
            :editable="false" :clearable="false" format="yyyy-MM-dd" value-format="timestamp">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="记录内容" prop="DateRemark">
          <el-input v-model="dataForm.DateRemark" placeholder="记录你将要做的一件事..." type="textarea"
            :rows="3" />
        </el-form-item>

        <el-form-item label="日期类型">
          <el-radio-group v-model="dataForm.DateType">
          <el-radio :label="0">工作日</el-radio>
          <el-radio :label="1">休息日</el-radio>
          <el-radio :label="2">法定假日</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <!-- <el-button type="danger" @click="handleDel()" v-if="dataForm.id">删 除</el-button> -->
        <el-button @click="visible = false">{{$t('common.cancelButton')}}</el-button>
        <el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">
          {{$t('common.confirmButton')}}</el-button>
      </span>
    </el-dialog>
  </template>
  
  <script>
  import restAPI from "@/api/q1/basedata/schedule";
  export default {
    data() {
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('结束时间不能为空'));
        } else {
          if (this.dataForm.startTime > value) {
            callback(new Error('结束时间应大于起始时间'));
          }
          callback();
        }
      };
      var validateRemark= (rule, value, callback) => {
          if (!value || value.trim().length == 0) {
        callback(new Error("记录内容不能为空"));
      } else {
        callback();
      }
    };

      return {
        visible: false,
        btnLoading: false,
        dataForm: {
          id: 0,
          year:undefined,
          DateType: 0,
          startTime: '',
          endTime: '',
          DateRemark: '',
        },
        dataRule: {
          startTime: [
            { required: true, message: '起始时间不能为空', trigger: 'change' }
          ],
          endTime: [
            { required: true, validator: validatePass, trigger: 'change' }
          ],
          DateRemark: [
            { required: true, validator: validateRemark, trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init(id, startTime,year) {
        this.dataForm.id = id || 0
        this.dataForm.year=year
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            restAPI.ScheduleInfo(this.dataForm.id).then(res => {
              this.dataForm = res.data
            })
          } else {
            this.dataForm.startTime = startTime || ''
          }
        })
      },
      dataFormSubmit() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.btnLoading = true
            if (!this.dataForm.id) delete (this.dataForm.id)
            restAPI.ScheduleUpdate(this.dataForm).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.btnLoading = false
                  this.$emit('refreshDataList')
                }
              })
            }).catch(() => { this.btnLoading = false })
          }
        })
      },
      handleDel() {
        this.$confirm('您确定要删除当前日程吗, 是否继续?', '提示', {
          type: 'warning'
        }).then(() => {
          restAPI.ScheduleDelete(this.dataForm.id).then(res => {
            this.$message({
              message: res.msg,
              type: 'success',
              duration: 1500,
            })
            this.visible = false
            this.$emit('refreshDataList')
          })
        }).catch(() => { });
      },
    }
  }
  </script>